package com.detu.ivos.vehicle.service.impl;

import com.detu.ivos.base.exception.ServiceException;
import com.detu.ivos.base.response.StatusCode;
import com.detu.ivos.vehicle.mapper.VehicleMapper;
import com.detu.ivos.vehicle.pojo.dto.VehicleQuery;
import com.detu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import com.detu.ivos.vehicle.pojo.entity.Vehicle;
import com.detu.ivos.vehicle.pojo.vo.VehicleVO;
import com.detu.ivos.vehicle.service.VehicleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {
        List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicle(vehicleQuery);
        return vehicleVOS;
    }

    @Override
    public void insertAndUpdateVehicle(VehicleSaveParam vehicleSaveParam) {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        if (vehicle.getId()!=null){
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }else{
            vehicle.setStatus("1");
            vehicle.setGeofenceBindStatus("0");
            vehicle.setCreateTime(new Date());
            int i=vehicleMapper.insertAndUpdateVehicle(vehicle);
            if (i<=0){
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
        }

    }


    //删除车辆

    @Override
    public void deleteVehicleById(Long id) {
        vehicleMapper.deleteVehicleById(id);
    }

    //解绑车辆对象
    @Override
    public void unbind(Long id) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setGeofenceId(null);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updatebind(vehicle);
    }
    //绑定车辆
    @Override
    public void bind(Long geoId,Long id) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setGeofenceId(geoId);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updatebind(vehicle);
    }
}
